package display.client;

import com.google.gwt.event.dom.client.ErrorEvent;
import com.google.gwt.event.dom.client.ErrorHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

public class IconLabel extends Composite {

    private Image image = new Image ();
    
    private Label label = new Label ();
    
    public IconLabel (String imageUrl, String labelText) {
        // Hook up an error handler, so that we can be informed if the image fails
        // to load.
        image.addErrorHandler(new ErrorHandler() {
          public void onError(ErrorEvent event) {
            label.setText("An error occurred while loading.");
          }
        });

        label.setText(labelText);
        image.setUrl(imageUrl);

        HorizontalPanel panel = new HorizontalPanel ();
        panel.add(image);
        panel.add(label);
        
        initWidget(panel);
    }

    public void setLabelText(String labelText) {
        label.setText(labelText);
    }

    public void setIconUrl(String imageUrl) {
        image.setUrl(imageUrl);
    }
    
    public int getHorizontalImageOffset() {
        if(image != null) {
            return (image.getOffsetWidth() / 2);
        }
        return 0;
    }
    
    public int getVerticalImageOffset() {
        if(image != null) {
            return (image.getOffsetHeight() / 2);
        }
        return 0;
    }
}
